<template>
  <div class="container">
    <div class="app-container">
      <div><el-button type="primary" @click="showVisible = true">添加角色</el-button></div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          label="角色"
          width="200px"
          align="center"
        >
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.newrow.name" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="启用"
          width="200px"
          align="center"
        >
          <template v-slot="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.newrow.state"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>{{ row.state === 1 ? '启用' :row.state === 0 ? '未启用':'无' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="描述"
          align="center"
        >
          <template v-slot="{row}">
            <el-input
              v-if="row.isEdit"
              v-model="row.newrow.description"
              type="textarea"
              size="mini"
            />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button size="mini" type="primary" @click="confirmEdit(row)">确 定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取 消</el-button>

            </template>
            <template v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" @click="edit(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="dellist(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>

            </template>

          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" style="height:60px" align="middle">
        <el-pagination
          layout="prev, pager, next"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page.sync="pageParams.page"
          @current-change="currentChangeFn"
        />
      </el-row>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog title="新增角色" :visible.sync="showVisible" width="500px">
      <el-form ref="addForm" :model="addform" :rules="rules" label-width="120px" @close="closeFn">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addform.name" autocomplete="off" style="width:300px" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch
            v-model="addform.state"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model="addform.description"
            style="width:300px"
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="submit">确 定</el-button>
              <el-button size="mini" @click="showVisible = false">取 消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, updataRole, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      showVisible: false,
      addform: {
        name: '',
        description: '',
        state: 1 // 角色状态，默认是启用的
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空哦', trigger: 'blur' }
        ],
        description:
        [
          { required: true, message: '角色名称不能为空哦', trigger: 'blur' }
        ]

      },
      tableData: [],
      pageParams: {
        page: 1,
        pagesize: 5,
        total: 0
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList({ ...this.pageParams })
      // console.log(rows)
      this.tableData = rows
      this.tableData.forEach(item => {
        // 用来表示是否正在编辑
        // item.isEdit = false // 直接添加属性不是响应式的
        this.$set(item, 'isEdit', false)
        this.$set(item, 'newrow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
      this.pageParams.total = total
    },
    currentChangeFn() {
      this.getRoleList()
    },
    submit() {
      this.$refs.addForm.validate(async pass => {
        if (pass) {
          await addRole({ ...this.addform })
          this.$message.success('恭喜你添加成功')
          this.getRoleList()
          this.closeFn()
        }
      })
    },
    // 给x绑定close 事件 ，点击后关闭表单效验，并且关闭表单
    closeFn() {
      this.$refs.addForm.resetFields()
      this.showVisible = false
    },
    // 更新角色点击取消，关闭input 框 并且给复制的数据重新赋值原来的
    edit(row) {
      row.isEdit = true
      row.newrow.name = row.name
      row.newrow.description = row.description
      row.newrow.state = row.state
    },
    // 跟新角色
    async confirmEdit(row) {
      if (!row.newrow.name || !row.newrow.description) {
        return this.$message.warning('角色名称和角色描述不能为空哦~~')
      }
      await updataRole({ ...row.newrow, id: row.id })
      this.$message.success('角色跟新成功')
      // 如果直接赋值，es链会报语法错误
      // 点击的时候直接赋值 isEdit: false关闭表单   ...row.newrow减少了刷新页面
      Object.assign(row, {
        isEdit: false,
        ...row.newrow
      })
    },
    async dellist(id) {
      await delRole(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1) {
        this.pageParams.page
      }
      this.getRoleList()
    }

  }
}
</script>
